package com.xiaoKe.zhonghzu.global.dialog;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewbinding.ViewBinding;

import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.global.base.AppRouter;
import com.xiaoKe.zhonghzu.global.base.OnOtherListener;
import com.xiaoKe.zhonghzu.global.base.RouterConfig;
import com.xiaoKe.zhonghzu.global.tool.HideIMEUtil;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.ui.onClick.NoDoubleClickListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;

public abstract class CenterDialog<T extends ViewBinding> extends DialogFragment {
    public T binding;
    public ToastDialog toastDialog;
    public List<String> permissionList = SPUtils.getList("permissionList");

    /**
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Type superclass = getClass().getGenericSuperclass();
        Class<T> aClass = (Class<T>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            binding = (T) method.invoke(null, getLayoutInflater());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        initParams();
        return binding.getRoot();

    }

    private void initParams() {
        Window window = getDialog().getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorTrans0)));
        int dialogHeight = getContextRect(getActivity());
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.MATCH_PARENT);
//        window.setWindowAnimations(R.style.DialogIOSAnim);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }

    //获取内容区域
    private int getContextRect(Activity activity) {
        //应用区域
        Rect outRect1 = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
        return outRect1.height();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        HideIMEUtil.wrap(Objects.requireNonNull(this));//键盘管理，点击除editText外区域收起键盘
        initView();
        initData();

        NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View view) {
                switch (view.getId()) {
                    case R.id.close:
                        dismiss();
                        break;
                    case R.id.affirm:
                        onOtherListener.onOther("affirm");
                        dismiss();
                        break;
                }
            }
        };
        if (null != getView().findViewById(R.id.affirm)) {
            getView().findViewById(R.id.affirm).setOnClickListener(noDoubleClickListener);
        }
        if (null != getView().findViewById(R.id.close)) {
            getView().findViewById(R.id.close).setOnClickListener(noDoubleClickListener);

        }

    }

    protected abstract void initView();

    protected abstract void initData();

    protected OnOtherListener onOtherListener;

    public void setOnOtherListener(OnOtherListener onOtherListener) {
        this.onOtherListener = onOtherListener;
    }


    @Override
    public void dismiss() {
        super.dismiss();
    }

    public boolean isShowing() {
        return isShowing;
    }

    private boolean isShowing = false;

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
            isShowing = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void toFragmentPage(RouterConfig routerConfig) {
        AppRouter.toFragment(getContext(), routerConfig);
    }

    protected void toFragmentForResult(RouterConfig routerConfig, int requestCode) {
        AppRouter.toFragmentForResult(this, routerConfig, requestCode);
    }

}
